package com.brillilab.starter.controller.crm;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.crm.CrmUsersInfoVo;
import com.brillilab.domain.vo.lab.LabApplyListVo;
import com.brillilab.domain.vo.user.UserAuditVo;
import com.brillilab.service.core.lab.ILabApplyService;
import com.brillilab.service.core.user.IUserInviteAuditService;
import com.brillilab.service.logic.crm.CrmUsersLogic;
import com.brillilab.service.logic.message.MessageLogic;
import com.brillilab.service.logic.user.UserInviteAuditLogic;
import com.brillilab.service.module.aliyun.AliYunSms;
import com.brillilab.starter.constant.StarterConstant;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zlb
 * @Title: UserAuditController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/6/28 19:11
 */
@RestController
@RequestMapping("/crm/userAudit")
public class UserAuditController {

    @Resource
    private ILabApplyService labApplyService;

    @Resource
    private IUserInviteAuditService userInviteAuditService;

    @Resource
    private CrmUsersLogic crmUsersLogic;

    @Resource
    private UserInviteAuditLogic userInviteAuditLogic;

    @Resource
    private MessageLogic messageLogic;

    @Resource
    private AliYunSms aliYunSms;


    /**
     * 成员申请列表
     *
     * @param token
     * @param labApplyListVo
     * @return
     */
    @PostMapping("/applyList")
    public ResponseVo applyList(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestBody LabApplyListVo labApplyListVo) {
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        Integer pageNum = labApplyListVo.getPageNum();
        Integer pageSize = labApplyListVo.getPageSize();
        return ResponseVo.success(userInviteAuditService.selectToState(labApplyListVo.getApplyState(), labApplyListVo.getMobile(), pageNum, pageSize));
    }

    /**
     * 获取成员申请信息
     *
     * @return
     */
    @GetMapping("/getApplyInfo")
    public ResponseVo getApplyInfo(Long applyId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
        //校验登录用户是否为CRM用户
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        Assert.isTrue(crmUsersLogic.checkTokenIsCrmUser(crmUsersInfoVo.getToken()), "不是CRM用户！");

        Assert.isTrue(applyId != null, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(userInviteAuditLogic.getUserInviteAuditById(applyId));
    }

    /**
     * 成员审核
     *
     * @param userAuditVo
     * @param token
     * @return
     */
    @PostMapping("/audit")
    public ResponseVo auditUser(@RequestBody UserAuditVo userAuditVo, @RequestHeader(name = StarterConstant.TOKEN) String token) {
        Assert.isTrue(StringUtils.isNotEmpty(token), "token不能为空！");
        Assert.isTrue(userAuditVo.getApplyId() != null, "请输入申请ID！");
        Assert.isTrue(userAuditVo.getApplyState() != null, "请选择审核状态！");
        if (userAuditVo.getApplyState() == 2 && StringUtils.isEmpty(userAuditVo.getAuditInfo())) {
            return ResponseVo.failure("请输入不通过原因！");
        }
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        return userInviteAuditLogic.auditUser(userAuditVo, crmUsersInfoVo);
    }
}
